<?php

class ajaxActions extends sfActions {

    public function executeAjaxLoadSubcategories(sfWebRequest $request){
        $category_id = $request->getParameter('category_id');
        $c = new Criteria();
        $c->add(CountryPeer::ID, $this->getUser()->getAttribute('scountry')->getId());
        $c->add(SubcategoryPeer::CATEGORY_ID, $category_id);
        $c->add(DetCountrySubcategoryPeer::IS_SHOWN, 1);
        $c->addJoin(DetCountrySubcategoryPeer::SUBCATEGORY_ID, SubcategoryPeer::ID);
        $c->addJoin(DetCountrySubcategoryPeer::COUNTRY_ID, CountryPeer::ID);
        $c->addDescendingOrderByColumn(SubcategoryPeer::ID);
        
        $subcategories_count = SubcategoryPeer::doCount($c);
        if($subcategories_count<=0){
            return sfView::NONE;
        }
        
        $this->subcategories = SubcategoryPeer::doSelectWithI18n($c);
    }

    public function executeAjaxLoadSubcategoriesFrm(sfWebRequest $request){
        $category_id = $request->getParameter('category_id');
        $c = new Criteria();
        $c->addDescendingOrderByColumn(SubcategoryPeer::ID);
        $c->add(SubcategoryPeer::CATEGORY_ID, $category_id);
        $this->subcategories = SubcategoryPeer::doSelectWithI18n($c);
    }

    public function executeAjaxHideInterests(sfWebRequest $request){
        $this->getUser()->getAttributeHolder()->remove('stmp1');
        return sfView::NONE;
    }

    public function executeAjaxComment(sfWebRequest $request){
        $id = $request->getParameter('id');
        $comment = $request->getParameter('comment');
        
        $objeto = new ArticleComment();
        $objeto->setComment($comment);
        $objeto->setArticleId($id);
        $objeto->setOwnerId($this->getUser()->getAttribute('suid'));
        $objeto->setCreatedAt(time());
        $objeto->save();

        $this->comment = $objeto;
    }

    public function executeAjaxDeleteComment(sfWebRequest $request){
        $id = $request->getParameter('id');
        ArticleCommentPeer::doDelete($id);
        return sfView::NONE;
    }

    public function executeAjaxArticleAutoSave(sfWebRequest $request){
        $id = $request->getParameter('id');
        $title = $request->getParameter('title');
        $country = $request->getParameter('country');
        $language = $request->getParameter('language');
        $type = $request->getParameter('type');
        $costPub = $request->getParameter('costPub');
        $costSpec = $request->getParameter('costSpec');
        $category = $request->getParameter('category');
        $subcategory = $request->getParameter('subcategory');
        $summary = $request->getParameter('summary');
        $content = $request->getParameter('content');
        $hd_is_response = $request->getParameter('hd_is_response');

        if($type=='1'){
            $cost_final = $costPub;
        }elseif($type=='2'){
            $cost_final = $costSpec;
        }

        $myLib = new myLib();
        $article = ArticlePeer::retrieveByPK($id);
        $article->setSlug($myLib->slugify($title));
        $article->setTitle($title);
        $article->setSummary($summary);
        $article->setContentFree($content);
        $article->setArticleTypeId($type);
        $article->setCountryId($country);
        $article->setLanguageId($language);
        if($category!=''){
            $article->setCategoryId($category);
        }
        if($subcategory!=''){
            $article->setSubcategoryId($subcategory);
        }
        $article->setOwnerId($this->getUser()->getAttribute('suid'));
        $article->setResponseOf($hd_is_response);
        $article->save();

        $c = new Criteria();
        $c->add(ArticleCostPeer::ARTICLE_ID, $article->getId());
        $c->addDescendingOrderByColumn(ArticleCostPeer::CREATED_AT);
        $article_cost = ArticleCostPeer::doSelectOne($c);

        if(!is_object($article_cost)){
            $article_cost = new ArticleCost();
        }
        $article_cost->setLastCost($cost_final);
        $article_cost->setArticle($article);
        $article_cost->setCreatedAt(date("Y-m-d H:i:s"));
        $article_cost->save();

        return sfView::NONE;
    }

    public function executeAjaxSaveScore(sfWebRequest $request){
        $id = $request->getParameter('id');/* article_id */
        $score = $request->getParameter('score');/* score */
        /* revisamos si el usuario ya hizo la puntuacion */
        $c = new Criteria();
        $c->add(ArticleScoresPeer::ACCOUNT_ID, $this->getUser()->getAttribute('suid'));
        $c->add(ArticleScoresPeer::ARTICLE_ID, $id);
        $article_score = ArticleScoresPeer::doSelectOne($c);
        /* si no la hizo, se graba */
        if(!is_object($article_score)){
            $new_article_score = new ArticleScores();
            $new_article_score->setAccountId($this->getUser()->getAttribute('suid'));
            $new_article_score->setArticleId($id);
            $new_article_score->setScore($score);
            $new_article_score->save();
            
            $obj = ArticlePeer::retrieveByPK($id);
            if($obj->getScores()!=''){
                $scores = $obj->getScores().'*'.$score;
            }else{
                $scores = $score;
            }
            $obj->setScores($scores);

            $suma=0;
            $count=0;
            $array_scores = preg_split('/[*]/', $scores);
            foreach($array_scores as $as){
                $suma+=$as;
                $count++;
            }

            $obj->setScore(floatval($suma/$count));
            $obj->save();
        }/* si ya habia puntuado, no pasa nada */
        return sfView::NONE;
    }

    public function executeAjaxAddFavorites(sfWebRequest $request){
        $id = $request->getParameter('id');/* article_id */
        $logged_user = AccountPeer::retrieveByPK($this->getUser()->getAttribute('suid'));
        /* myFavoriteArticles */
        $array_suattr = $this->getUser()->getAttribute('suattr');
        if($array_suattr[7]!=''){
            $favArticles = $array_suattr[7] .= '*'.$id;
        }else{
            $favArticles = $id;
        }
        $array_suattr[7] = $favArticles;
        $logged_user->setFavoriteArticles($favArticles);
        $logged_user->save();
        $this->getUser()->setAttribute('suattr',$array_suattr);
        $this->article = ArticlePeer::retrieveByPK($id);
    }

    public function executeAjaxRemoveFavorites(sfWebRequest $request){
        $id = $request->getParameter('id');/* article_id */
        /* myFavoriteArticles */
        $array_suattr = $this->getUser()->getAttribute('suattr');
        $array_favArticles = preg_split('/[*]/', $array_suattr[7]);
        $index = array_search($id, $array_favArticles);
        unset($array_favArticles[$index]);
        $array_favArticles = array_values($array_favArticles);
        $string = "";
        $count = 0;
        foreach($array_favArticles as $fa){
            if($count==0){
                $string .= $fa;
            }else{
                $string .= "*".$fa;
            }
            $count++;
        }
        $logged_user = AccountPeer::retrieveByPK($this->getUser()->getAttribute('suid'));
        $logged_user->setFavoriteArticles($string);
        $logged_user->save();
        $array_suattr[7] = $string;
        $this->getUser()->setAttribute('suattr',$array_suattr);
        $this->article = ArticlePeer::retrieveByPK($id);
    }

    public function executeAjaxSubscribe(sfWebRequest $request){
        $id = $request->getParameter('id');/* autor id */

        $logged_user = AccountPeer::retrieveByPK($this->getUser()->getAttribute('suid'));
        if($logged_user->getSubscriptions()!=''){
            $subscriptions = $logged_user->getSubscriptions().'*'.$id;
        }else{
            $subscriptions = $id;
        }
        $logged_user->setSubscriptions($subscriptions);
        $logged_user->save();

        $account_subscriptions = new AccountSubscriptions();
        $account_subscriptions->setUserId($id);/* perosna a la que se suscribe */
        $account_subscriptions->setSubscriberId($this->getUser()->getAttribute('suid'));/* logueado */
        $account_subscriptions->save();

        $array_suattr = $this->getUser()->getAttribute('suattr');
        $array_suattr[6] = $subscriptions;
        $this->getUser()->setAttribute('suattr',$array_suattr);

        $this->author_id = $id;
    }

    public function executeAjaxUnsubscribe(sfWebRequest $request){
        $id = $request->getParameter('id');/* autor id */

        //eliminar el id del suscriptor
        $array_suattr = $this->getUser()->getAttribute('suattr');
        $array_subscriptions = preg_split('/[*]/', $array_suattr[6]);

        $index = array_search($id, $array_subscriptions);
        unset($array_subscriptions[$index]);
        $array_subscriptions = array_values($array_subscriptions);

        $string = "";
        $count = 0;
        foreach($array_subscriptions as $as){
            if($count==0){
                $string .= $as;
            }else{
                $string .= "*".$as;
            }
            $count++;
        }

        $logged_user = AccountPeer::retrieveByPK($this->getUser()->getAttribute('suid'));
        $logged_user->setSubscriptions($string);
        $logged_user->save();

        $c = new Criteria();
        $c->add(AccountSubscriptionsPeer::USER_ID, $id);
        $c->add(AccountSubscriptionsPeer::SUBSCRIBER_ID, $this->getUser()->getAttribute('suid'));
        AccountSubscriptionsPeer::doDelete($c);

        $array_suattr[6] = $string;
        $this->getUser()->setAttribute('suattr',$array_suattr);

        $this->author_id = $id;
    }

    public function executeAjaxReportAbuse(sfWebRequest $request){
        $abuse_id = $request->getParameter('abuse_id');
        $article_id = $request->getParameter('article_id');
        $message = $request->getParameter('message');

        $abuse = new ArticleAbuse();
        $abuse->setMessage($message);
        $abuse->setAbuseId($abuse_id);
        $abuse->setArticleId($article_id);
        $abuse->setOwnerId($this->getUser()->getAttribute('suid'));
        $abuse->setCreatedAt(time());
        $abuse->save();

        return sfView::NONE;
    }
    
}